<template>
  <div class="aui-wrapper aui-page__login">
    <div class="aui-content__wrapper">
      <main class="aui-content">
        <div class="login-header">
          <h2 class="login-brand">{{ $t("brand.lg") }}</h2>
        </div>
        <div class="login-body">
          <h3 class="login-title">{{ $t("login.title") }}</h3>
          <el-form
            :model="dataForm"
            :rules="dataRule"
            ref="dataForm"
            @keyup.enter.native="dataFormSubmitHandle()"
            status-icon
          >
            <!--<el-form-item>-->
            <!--<el-select v-model="$i18n.locale" class="w-percent-100">-->
            <!--<el-option-->
            <!--v-for="(val, key) in i18nMessages"-->
            <!--:key="key"-->
            <!--:label="val._lang"-->
            <!--:value="key"-->
            <!--&gt;</el-option>-->
            <!--</el-select>-->
            <!--</el-form-item>-->
            <el-form-item prop="username">
              <el-input
                v-model="dataForm.username"
                :placeholder="$t('login.username')"
              >
                <span slot="prefix" class="el-input__icon">
                  <svg class="icon-svg" aria-hidden="true">
                    <use xlink:href="#icon-user"></use>
                  </svg>
                </span>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="dataForm.password"
                type="password"
                :placeholder="$t('login.password')"
              >
                <span slot="prefix" class="el-input__icon">
                  <svg class="icon-svg" aria-hidden="true">
                    <use xlink:href="#icon-lock"></use>
                  </svg>
                </span>
              </el-input>
            </el-form-item>
            <el-form-item prop="usertype">
              <el-select
                v-model="dataForm.usertype"
                class="w-percent-100"
                clearable
              >
                <el-option
                  v-for="i in typeOption"
                  :label="i.label"
                  :key="i.value"
                  :value="i.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="captcha">
              <el-row :gutter="20">
                <el-col :span="14">
                  <el-input
                    v-model="dataForm.captcha"
                    :placeholder="$t('login.captcha')"
                  >
                    <span slot="prefix" class="el-input__icon">
                      <svg class="icon-svg" aria-hidden="true">
                        <use xlink:href="#icon-safetycertificate"></use>
                      </svg>
                    </span>
                  </el-input>
                </el-col>
                <el-col :span="10" class="login-captcha">
                  <img :src="captchaPath" @click="getCaptcha()" />
                </el-col>
              </el-row>
            </el-form-item>
            <el-form-item>
              <el-button
                style="background: #128eff"
                type="primary"
                @click="dataFormSubmitHandle()"
                class="w-percent-100"
                >{{ $t("login.title") }}</el-button
              >
            </el-form-item>
          </el-form>
          <div style="text-align: right; margin-top: -8px">
            <span class="forgetPwd" @click="forget()">忘记密码</span>
          </div>
        </div>
        <!-- <div class="login-footer">
          <p>
            <a href="#" target="_blank">{{ $t("login.demo") }}</a>
          </p>
          <p>
            <a href="#" target="_blank">{{ $t("login.copyright") }}</a>
            皖ICP备2022000199号
          </p>
        </div> -->
      </main>
    </div>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import debounce from "lodash/debounce";
import { messages } from "@/i18n";
import { getUUID } from "@/utils";
export default {
  data() {
    return {
      typeOption: [
        {
          value: "1",
          label: "酒店民宿",
        },
        {
          value: "2",
          label: "商家",
        },
        {
          value: "3",
          label: "管理端",
        },
      ],
      i18nMessages: messages,
      captchaPath: "",
      dataForm: {
        username: "",
        password: "",
        uuid: "",
        captcha: "",
        grant_type: "password",
        usertype: "",
      },
      sendIng: false,
    };
  },
  computed: {
    dataRule() {
      return {
        username: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        captcha: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        usertype: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
      };
    },
  },
  created() {
    this.getCaptcha();
    this.userClear();
  },
  methods: {
    userClear() {
      localStorage.setItem("homestayId", "");
      localStorage.setItem("homestayName", "");
      localStorage.setItem("homestayIdArr", []);
      localStorage.setItem("MerchantId", "");
      localStorage.setItem("MerchantName", "");
      localStorage.setItem("MerchantIdArr", []);
      localStorage.setItem("ss_merchantCategory", "");
      localStorage.setItem("currentMenuList", "");
      localStorage.setItem("currentPermissions", "");
      this.$store.state.user.homestayId = "";
      this.$store.state.user.homestayName = "";
      this.$store.state.user.MerchantName = "";
      this.$store.state.user.MerchantId = "";
      this.$store.state.user.homestayIdArr = [];
      this.$store.state.user.MerchantIdArr = [];
    },
    forget() {
      this.$router.push({
        name: "forgetPwd",
      });
    },
    getTypeByDict() {
      this.$http.get("/sys/dict/data/getListByTypeOne").then((res) => {});
    },
    // 获取验证码
    getCaptcha() {
      this.dataForm.uuid = getUUID();
      this.captchaPath = `${window.SITE_CONFIG["apiURL"]}/auth/captcha?uuid=${this.dataForm.uuid}`;
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        if (!this.sendIng) {
          this.sendIng = true;
        } else {
          return;
        }
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }

          let newData = JSON.parse(JSON.stringify(this.dataForm));
          newData.password = this.$CryptoJS.encrypt(newData.password);

          this.$http
            .post(
              "/auth/oauth/token",
              {
                ...newData,
                username:
                  this.dataForm.username + "&&" + `${this.dataForm.usertype}`,
              },
              {
                headers: {
                  "content-type": "application/x-www-form-urlencoded",
                  Authorization: "Basic cmVucmVuaW86cmVucmVuaW8=",
                },
              }
            )
            .then(({ data: res }) => {
              this.sendIng = false;
              if (res.code !== 0) {
                this.getCaptcha();
                return this.$message.error(res.msg);
              }
              Cookies.set("access_token", res.access_token);
              // if (this.dataForm.usertype == '1') {
              //     this.$router.replace({
              //         name: "hotelHome"
              //     });
              // } else {
              //     this.$router.replace({
              //         name: "home"
              //     });
              // }
              this.$store.state.user.type = this.dataForm.usertype;
              localStorage.setItem("usertype", this.dataForm.usertype);
              this.$router.replace({
                name: "home",
              });
            })
            .catch(() => {});
        });
      },
      1000,
      {
        leading: true,
        trailing: false,
      }
    ),
  },
};
</script>

<style lang="scss" scoped>
.aui-page__login {
  background: url("../../assets/img/loginBg.png");
  background-size: cover;
}
.forgetPwd {
  color: #128eff;
  font-size: 14px;
}
</style>
